package com.example.design.deprecated.state;

import com.example.design.utils.RedisUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class DeprecatedSendOrder extends DeprecatedAbstractOrderState{

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private DeprecatedReceiveOrder receiveOrder;

    @Override
    protected DeprecatedOrder sendOrder(String orderId, DeprecatedOrderContext context) {
        DeprecatedOrder order = (DeprecatedOrder) redisUtils.get(orderId);
        if (!order.getState().equals(ORDER_WAIT_SEND)){
            throw new UnsupportedOperationException("Order state should be ORDER_WAIT_SEND" +
                    ".But now it's state is : " + order.getState());
        }
        //点击发货后，修改订单状态为 待收货，并更新Redis缓存
        order.setState(ORDER_WAIT_RECEIVE);
        redisUtils.set(orderId,order);
        //todo 观察者模式：发送订单发货Event（4.5小节和4.6小节进行实现）
        //设置上下文的状态为待收货
        super.notifyObserver(orderId,ORDER_WAIT_RECEIVE);
        return order;
    }
}
